package com.wzt.lianfirecontrol.http.json;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.wzt.lianfirecontrol.activity.ActivityCollecter;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.activity.LoginActivity;
import com.wzt.lianfirecontrol.bean.recode.RequestModel;
import com.wzt.lianfirecontrol.bean.recode.user.UserInfoModel;
import com.wzt.lianfirecontrol.utils.JSONUtil;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.ToastUtils;

import org.json.JSONObject;

public abstract class ParseJsonData {
    public static final String ERROR_CODE = "code";
    public static final String REQUEST_OK_CODE = "10000";
    // token已过期，请重新登录
    public static final String RELOGIN_CODE = "10003";
    // 无效token，请重新登录
    public static final String RELOGIN_CODE_1 = "10002";
    public static final String RELOGIN_CODE_2 = "10004";
    public static final String RELOGIN_CODE_3 = "10008";// 禁用账户
    public static final String ERROR_MSG = "msg";
    public static final String REQUEST_OK = "requset_ok";
    public static final String ANALYZE_ERROR_MSG = "数据解析异常";
    public static final String DATA = "data";
    public boolean isAllDataStr = false;

    public void parseResponseWithGSON(String jsonData, final BaseActivity activity, Handler handler, int what){
        Message message = handler.obtainMessage();
        message.what = what;
        Bundle bundle = new Bundle();
        bundle.putBoolean(REQUEST_OK, false);
        bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
        if (isAllDataStr){
            analyzeResult(jsonData, bundle, what);
            message.setData(bundle);
            handler.sendMessage(message);
            return;
        }
        boolean isNeedSendMsg = true;
        try {
            JSONObject jsonObject = new JSONObject(jsonData);
            RequestModel requestModel = JSONUtil.jsonObjectToBean(jsonObject, RequestModel.class);
            if (null != requestModel){
                bundle.putString(ERROR_CODE, requestModel.getError_code());
                String error_msg = requestModel.getError_msg();
                if (!StringUtils.isEmpty(error_msg)){
                    final String finalError_msg = error_msg;
                    String code = requestModel.getError_code();
                    if (!(REQUEST_OK_CODE.equals(code))){
                        if ((RELOGIN_CODE.equals(code))
                                || (RELOGIN_CODE_1.equals(code))
                                || (RELOGIN_CODE_2.equals(code))
                                || (RELOGIN_CODE_3.equals(code))){
                            if (activity instanceof LoginActivity && !(RELOGIN_CODE_1.equals(code))){
                                activity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        ToastUtils.showToast(activity, finalError_msg);
                                    }
                                });
                            }
                        }else {
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ToastUtils.showToast(activity, finalError_msg);
                                }
                            });
                        }

                    }
                }else {
                    error_msg = ANALYZE_ERROR_MSG;
                }
                if (REQUEST_OK_CODE.equals(requestModel.getError_code())){
                    String data = jsonObject.getString(DATA);
                    if (!StringUtils.isEmpty(data)) {
                        analyzeResult(data, bundle, what);
                    }else {
                        bundle.putString(ERROR_MSG, error_msg);
                    }
                }else if ((RELOGIN_CODE.equals(requestModel.getError_code())
                        || RELOGIN_CODE_1.equals(requestModel.getError_code())
                        || RELOGIN_CODE_2.equals(requestModel.getError_code())
                        || RELOGIN_CODE_3.equals(requestModel.getError_code()))
                        && !(ActivityCollecter.getCurrentActivity() instanceof LoginActivity)){
                    isNeedSendMsg = false;
                    handler.removeCallbacksAndMessages(null);
                    UserInfoModel.clearUserLoginMessage(activity);
                    ActivityCollecter.finishAllActivitys();
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("code_msg", error_msg);
                    activity.openActivity(LoginActivity.class, bundle1, 0);
                }else {
                    bundle.putString(ERROR_MSG, error_msg);
                }
            }else {
                bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
            }
        }catch (Exception e){
            bundle.putString(ERROR_MSG, ANALYZE_ERROR_MSG);
        }finally {
            if (isNeedSendMsg) {
                message.setData(bundle);
                handler.sendMessage(message);
            }
        }
    }

    /**
     * 对应数据的具体解析
     * @param dataStr
     * @param bundle
     * @param what
     */
    public abstract void analyzeResult(String dataStr, Bundle bundle, int what);
}
